package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class SzlhxqGovNewsAnalyse implements AnalyseNews {

	private static final String[] regex={

//		http://www.szlhxq.gov.cn/lhxinqu/zdlyxxgkzl/zjxx11/czyjs12/796353/index.html
		"http://www.szlhxq.gov.cn/.*/.*/.*/[0-9]+/index.html",
		"http://www.szlhxq.gov.cn/eportal/ui\\?pageId=[0-9]+&msgDataId=[a-zA-Z0-9]+"
	};


	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String src_url=urlMeta.getSrc_url();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);
		

		if(url.startsWith("http://www.szlhxq.gov.cn/eportal/ui")){
			title =doc.select("table.hdxl>tbody>tr:eq(0)>td").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("table.hdxl"));
			date=DateUtils.matchDate(doc.select("td.hyxbt").text());
		}else{
			title =doc.select("div.min h1").text();
			if (StringUtils.isBlank(title)){
				title=doc.select("div.zxft_title").text();
			}
			/**
			 * 作者
			 */
			 author=JSoupUtils.matchAuthor(doc, "来源：");
			 doc.select("p.gn_btn").remove();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.min p"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.ftsl_con"));
			}
			date=DateUtils.matchDate(doc.select("div.fbsj").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.zxft_jb").text());
			}
		}
		if (StringUtils.isNotBlank(title) && date!=null){
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setDate(date);
			news.setAuthor(author);
			news.setType(1);
		}
		return news;
	}
	public static void main(String[] args) {
		System.out.println(DoMainUtils.GetDomainName("http://lh.sz.edu.cn/ShowArticle.asp?ArticleID=0806c03b-94a3-4d22-b764-8137c5f8d9f5"));
		String url="http://www.szlhxq.gov.cn/lhxinqu/gzcy/cjwtjd/503602/index.html";
		SzlhxqGovNewsAnalyse analyse=new SzlhxqGovNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
